package com.zut.controller;


import com.zut.mapper.ManagerMapper;
import com.zut.pojo.Login;
import com.zut.pojo.Manager;
import com.zut.pojo.Result;
import com.zut.service.ManagerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;


/**
 * 登录控制器
 *
 * @author zhoubin
 * @since 1.0.0
 */

@Api(tags = "LoginController")
@RestController
public class LoginController {
    @Autowired
    ManagerService managerService;


    @ApiOperation(value = "登录之后返回token")
    @PostMapping("/login")
    public Result login(@RequestBody Login login, HttpServletRequest request) {
        return managerService.login(login.getUsername(), login.getPassword(), login.getCode(), request);
    }


    @ApiOperation(value = "获取当前登录用户的信息")
    @GetMapping("/manager/info")
    public Manager getManagerInfo(Principal principal) {
        if (null == principal) {
            return null;
        }
        String username = principal.getName();
        Manager manager = managerService.getManagerByUserName(username);
        manager.setPassword(null);
        return manager;
    }

    @ApiOperation(value = "注册")
    @PostMapping("/register")
    public Result register(@RequestBody Login login, HttpServletRequest request) {
        return managerService.register(login.getUsername(), login.getPassword(), login.getCode(), login.getEmail(), request);
    }


    @ApiOperation(value = "退出登录")
    @PostMapping("/logout")
    public Result logout() {
        return Result.success("注销成功");
    }

}
